local rule = fk.CreateSkill {
  name = "#hx__AWEC_rule&",
}

local U = require "packages/utility/utility"
local HX = require "packages/huixiangkill/hxfs"

Fk:loadTranslationTable{
  ["@@hx__AWEC_mode-Suit"] = "主",
}

--手牌无上限
rule:addEffect("maxcards", {
  correct_func = function(self, player)
    return player and 999
  end
})

--初始手牌0张
rule:addEffect(fk.DrawInitialCards, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    data.num = 0
  end,
})

--禁止摸牌
rule:addEffect(fk.DrawNCards, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    data.n = 0
  end,
})

--宝宝提示
rule:addEffect(fk.AfterCardsMove, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    if Fk:currentRoom():getBanner("hx__AWEC_mode-Lord") == nil then return end
    local cardsget = {}
    local cardsgive = {}
    for _, move in ipairs(data) do
      if move.to and move.to == player then
        for _, info in ipairs(move.moveInfo) do
          table.insert(cardsget, info.cardId)
        end
      end
      if move.from and move.from == player then
        for _, info in ipairs(move.moveInfo) do
          table.insert(cardsgive, info.cardId)
        end
      end
    end
    cardsget = player.room.logic:moveCardsHoldingAreaCheck(cardsget)
    cardsgive = player.room.logic:moveCardsHoldingAreaCheck(cardsgive)
    if #cardsget > 0 then
      return player.id == table.random(Fk:currentRoom().players).id
    end
    if #cardsgive > 1 then
      return player.id == table.random(Fk:currentRoom().players).id
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local log = {
      "点数遵循2＜3＜4＜5＜6＜7＜8＜9＜10＜J＜Q＜K＜A＜主牌",
      "花色要相同，但是主牌和王可以无视花色",
      "压牌时，如果使用了王来压牌，则其他玩家不可再跟牌，直接开始下一轮",
      "跟牌可以跟处理区出现的点数，当然也可以直接点确定，即不跟牌",
      "攻牌要考虑的就很少了，除了王不可以用来攻，只要出点数都一样的牌就可以",
      "小技巧，如果压完牌后没有手牌了，其他玩家也不能再跟牌了，直接下一轮",
      "不足八张牌是要摸到八张的，牌堆是不会洗牌的",
      "率先打空手牌，就是赢家",
      "前期收点牌没问题，后期还收就有问题了",
      "收牌是门学问",
      "摸牌也是有顺序的，攻牌的人先摸，压牌的再摸，之后再按座位",
      "4人局一般不建议跟对面的玩家",
      "跟牌是门学问",
      "压不下牌不要担心，收牌就好了",
      "规则其实很简单，看你有没有认真读了",
      "猜猜出现这个随机聊天的时机是什么",
      "点数遵循2＜3＜4＜5＜6＜7＜8＜9＜10＜J＜Q＜K＜A＜主牌",
      "花色要相同，但是主牌和王可以无视花色",
      "压牌时，如果使用了王来压牌，则其他玩家不可再跟牌，直接开始下一轮",
      "跟牌可以跟处理区出现的点数，当然也可以直接点确定，即不跟牌",
      "攻牌要考虑的就很少了，除了王不可以用来攻，只要出点数都一样的牌就可以",
      "小技巧，如果压完牌后没有手牌了，其他玩家也不能再跟牌了，直接下一轮",
      "点数遵循2＜3＜4＜5＜6＜7＜8＜9＜10＜J＜Q＜K＜A＜主牌",
      "花色要相同，但是主牌和王可以无视花色",
      "压牌时，如果使用了王来压牌，则其他玩家不可再跟牌，直接开始下一轮",
      "跟牌可以跟处理区出现的点数，当然也可以直接点确定，即不跟牌",
      "攻牌要考虑的就很少了，除了王不可以用来攻，只要出点数都一样的牌就可以",
      "小技巧，如果压完牌后没有手牌了，其他玩家也不能再跟牌了，直接下一轮",
      "摸牌也是有顺序的，攻牌的人先摸，压牌的再摸，之后再按座位",
    }
    table.insert(log, "抽到这句话的概率是"..#log.."分之一")
    player:chat(table.random(log))
  end,
})

--游戏流程
rule:addEffect(fk.GameStart, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return player.role == "lord"
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local players = room.alive_players
    table.sort(players, function(a, b) return a.seat < b.seat end)
    --获取并设置主牌
    local pileNum = room:getBanner("hx__AWEC_mode-piles")
    local allCardIds = Fk:getAllCardIds()
    local allcards = {}
    local white_cards = {"huixiang__poker"}
    if pileNum > 1 then
      for j = 1, pileNum - 1 do
        for i = #allCardIds, 1, -1 do
          local id = allCardIds[i]
          local card = Fk:getCardById(id)
          if card then
            if table.contains(white_cards, card.package.name) then
              if not card.is_derived then
                table.insert(allcards, {card.name, card.suit, card.number})
              end
            end
          end
        end
      end
      room:changeCardArea(U.prepareDeriveCards(room, allcards, self.name), Card.DrawPile)
    end
    if #room.draw_pile < 10 then
      room:sendLog{ type = "未开启<b>扑克牌</b>卡组，无法开始游戏！", toast = true }
      room:delay(2000)
      room:gameOver("")
    end
    local cards = HX.getCardsFromPileByRule(room, ".|.|^nosuit", 4,"drawPile", #players * 8)
    local car = Fk:getCardById(cards[1])
    room:sendLog{
      type = "本局的主牌为%card，花色为%arg",
      card = {car.id},
      arg = car:getSuitString(false),
      toast = true,
    }
    room:delay(2000)
    room:setBanner("hx__AWEC_mode-Suit", car.suit)
    room:setBanner("@hx__AWEC_mode-Suit", car:getSuitString(false))
    for _, c in ipairs(room.draw_pile) do
      local C = Fk:getCardById(c, true)
      if C.suit == car.suit then
        room:setCardMark(C, "@@hx__AWEC_mode-Suit", 1)
      end
    end
    --发牌8张
    for i = 1, 8 do
      room:broadcastPlaySound("./packages/huixiangkill/audio/card/refreshcards")
      for _, p in ipairs(players) do
        room:delay(150)
        local card = HX.AWEC_drawCards(room, p, 1, self.name)
        if card[1] == car.id then
          room:setBanner("hx__AWEC_mode-Lord", p.id)
          room:sendLog{
            type = "玩家 %from 抽中了主牌 %card",
            from = p.id,
            card = {car.id},
            toast = true,
          }
        end
      end
    end
    --重新分配座位
    local lordid = room:getBanner("hx__AWEC_mode-Lord") or player.id
    local player_circle = {}
    local pn = 1
    for i = 1, #players do
      if players[i].id == lordid then
        pn = i
        break
      end
    end
    for j = pn, #players do
      table.insert(player_circle, players[j])
    end
    for j = 1, pn - 1 do
      table.insert(player_circle, players[j])
    end
    room:arrangeSeats(player_circle)

    --开始对局
    HX.AWEC_AWEC(room, player, lordid, players, self.name)
  end,
})

rule:addEffect(fk.TurnEnd, {  --发生未知bug导致循环中断的补救措施
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return player.role == "lord"
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local players = room.alive_players
    table.sort(players, function(a, b) return a.seat < b.seat end)
    local lordid
    --开始对局
    HX.AWEC_AWEC(room, player, lordid, players, self.name)
  end,
})



return rule
